package com.nsgf.library.ui.adapter;

import android.content.Context;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;

import java.util.List;


/**
 * Created by  on 2024/12/10.
 */

public class XFragmentAdapter extends FragmentPagerAdapter {
    private long baseId = 0;
    private Context mContext;
    private List<Fragment> mFragmentList;
    private String[] mPageTitleList;
    private int[] titles;
    private List<Integer> mBadgeCountList;
    FragmentManager fm;

    public XFragmentAdapter(Context context,
                            FragmentManager fm,
                            List<Fragment> fragmentList,
                            String[] pageTitleList,
                            List<Integer> badgeCountList) {
        super(fm);
        this.mContext = context;
        this.mFragmentList = fragmentList;
        this.mPageTitleList = pageTitleList;
        this.mBadgeCountList = badgeCountList;
        this.fm = fm;
    }

    public XFragmentAdapter(Context context,
                            FragmentManager fm,
                            List<Fragment> fragmentList,
                            String[] pageTitleList) {
        super(fm);
        this.mContext = context;
        this.mFragmentList = fragmentList;
        this.mPageTitleList = pageTitleList;
        this.fm = fm;
    }

    public XFragmentAdapter(Context context,
                            FragmentManager fm,
                            List<Fragment> fragmentList,
                            int[] pageTitleList) {
        super(fm);
        this.mContext = context;
        this.mFragmentList = fragmentList;
        this.titles = pageTitleList;
        this.fm = fm;
    }

    public void setFragments(List<Fragment> fragments) {
        if (this.mFragmentList != null) {
            FragmentTransaction ft = fm.beginTransaction();
            for (Fragment f : this.mFragmentList) {
                ft.remove(f);
            }
            ft.commit();
            ft = null;
            fm.executePendingTransactions();
        }

        this.mFragmentList = fragments;
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mPageTitleList[position];
    }

    @Override
    public long getItemId(int position) {
        // give an ID different from position when position has been changed
        return baseId + position;
    }

    /**
     * 更新fragment的数量之后，在调用notifyDataSetChanged之前，changeId(1) 改变id，改变tag
     *
     * @param n
     */
    public void changeId(int n) {
        // shift the ID returned by getItemId outside the range of all previous fragments
        baseId += getCount() + n;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

}
